package net.jxtz.jxtzos.controller.auth;


import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.auth.User;
import net.jxtz.jxtzos.entity.web.R;
import net.jxtz.jxtzos.mapper.redis.RedisMapper;
import net.jxtz.jxtzos.service.auth.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/authentication")
@Slf4j
public class AuthController {

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public R register(@RequestBody User user){
        String password = user.getPassword();
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);
        boolean save = userService.save(user);
        return save? R.ok(): R.error();
    }



    @Lazy
    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    private final UserService userService;

    @Autowired
    public AuthController(BCryptPasswordEncoder bCryptPasswordEncoder, UserService userService, RedisMapper redisMapper){
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.userService = userService;
    }

}
